<?php

namespace app\common\model;

use app\iindex\controller\Home;
use think\Db;
use think\Model;

/**
 * 文档基础模型
 */
class Member extends Model
{
    protected $auto = [];
    protected $insert = [
        'status' => 1,
        'userrank' => 0,
        'oldrank' => 0,
        'active_time',
        'update_time'
    ];
    protected $update = ['update_time'];

    /**
     * 设置注册时间
     * @return int
     */
    protected function setActiveTimeAttr()
    {
        return time();
    }

    /**
     * 设置更新时间
     * @return int
     */
    protected function setUpdateTimeAttr()
    {
        return time();
    }

    public function updateMember($uid, $userdata)
    {
        $map['uid'] = $uid;
        $result = $this->where($map)->save($userdata);
        if (!$result) {
            $this->error = '前台用户信息注册失败，请重试！';
            return false;
        } else {
            return true;
        }
    }

    /**
     * 会员登录
     * @param $uid
     * @param bool $remember
     * @return bool
     */
    public function login($uid, $remember = false)
    {
        /* 检测是否在当前应用注册 */
        $user = Db('member')->find($uid);
        if (!$user) { //未注册
            $this->error = '该用户尚未注册！';
            return false;
        } elseif ($user['status'] < 0) {
            $this->error = '用户被冻结！'; //应用级别禁用
            return false;
        }

        //记录行为
        action_log('user_login', 'member', $uid, $uid);
        return true;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout()
    {
        session('ifpsd', null);
        session('user_auth', null);
    }

    /**
     * 同步登陆时添加用户信息
     * @param $uid
     * @param $info
     * @return mixed
     * autor:xjw129xjt
     */
    public function addSyncData($uid, $info)
    {

        $data1['nickname'] = mb_substr($info['nick'], 0, 11, 'utf-8');
        //去除特殊字符。
        $data1['nickname'] = preg_replace('/[^A-Za-z0-9_\x80-\xff\s\']/', '', $data1['nickname']);
        empty($data1['nickname']) && $data1['nickname'] = $this->rand_nickname();
        $data1['nickname'] .= '_' . $this->rand_nickname();
        $data1['sex'] = $info['sex'];
        $data = $this->create($data1);
        $data['uid'] = $uid;
        $res = $this->insert($data);
        return $res;
    }

    public function rand_nickname()
    {
        $nickname = $this->create_rand(4);
        if ($this->where(array('nickname' => $nickname))->select()) {
            $this->rand_nickname();
        } else {
            return $nickname;
        }
    }

    function create_rand($length = 8)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }

    /**
     * 关联账户表
     * @return \think\model\relation\HasOne
     */
    public function finance()
    {
        return $this->hasOne('MemberFinance', 'uid', 'uid');
    }

    public function level()
    {
        return $this->belongsTo('Levels', 'userrank', 'id');
    }

    public function views()
    {
        return $this->hasMany('MemberViews', 'uid', 'uid');
    }

}
